package com.pmcc.core.config.utils;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * Created by LvXL on 2017/9/4.
 */
public class PageRequest implements Pageable, Serializable {

    private final Sort sort;
    protected int page = 1; // 当前页码
    protected int rows = 15; // 每页数量

    public PageRequest(int page, int pageSize) {
        this(page, pageSize, null);
    }

    public PageRequest() {

        this(1, 15, null);
    }

    public PageRequest(int page, int pageSize, Sort.Direction direction, String... properties) {
        this(page, pageSize, new Sort(direction, properties));
    }

    public PageRequest(int page, int rows, Sort sort) {
        this.page = page;
        this.rows = rows;
        this.sort = sort;
    }


    @Override
    public int getPageNumber() {
        return page;
    }

    @Override
    public int getPageSize() {
        return rows;
    }

    @Override
    public int getOffset() {
        return (page - 1) * rows;
    }

    public Sort getSort() {
        return sort;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#next()
     */
    public Pageable next() {
        return new org.springframework.data.domain.PageRequest(getPageNumber() + 1, getPageSize(), getSort());
    }

    @Override
    public Pageable previousOrFirst() {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.AbstractPageRequest#previous()
     */
    public PageRequest previous() {
        return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Pageable#first()
     */
    public Pageable first() {
        return new org.springframework.data.domain.PageRequest(0, getPageSize(), getSort());
    }

    @Override
    public boolean hasPrevious() {
        return false;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {

        if (this == obj) {
            return true;
        }

        if (!(obj instanceof org.springframework.data.domain.PageRequest)) {
            return false;
        }

        PageRequest that = (PageRequest) obj;

        boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);

        return super.equals(that) && sortEqual;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode());
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(),
                sort == null ? null : sort.toString());
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }
}
